package com.demo.blog;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.demo.common.model.Blog;
import com.demo.common.model.WeisiUser;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;

/**
 * BlogController
 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
@Before(BlogInterceptor.class)
public class BlogController extends Controller {
	
	static BlogService service = new BlogService();
	
	public void index() {
		setAttr("blogPage", service.paginate(getParaToInt(0, 1), 10));
		render("blog.html");
	}
	
	@Clear
	public void family() {
		setAttr("familyPage", service.familyPaginate(getParaToInt(0, 1), 10));
		render("family.html");
	}
	
	public void add() {
	}
	
	/**
	 * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
	 * 并要对数据进正确性进行验证，在此仅为了偷懒
	 */
	@Before(BlogValidator.class)
	public void save() {
		getModel(Blog.class).save();
		redirect("/blog");
	}
	
	public void edit() {
		setAttr("blog", service.findById(getParaToInt()));
	}
	
	/**
	 * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
	 * 并要对数据进正确性进行验证，在此仅为了偷懒
	 */
	@Before(BlogValidator.class)
	public void update() {
		getModel(Blog.class).update();
		redirect("/blog");
	}
	
	public void delete() {
		service.deleteById(getParaToInt());
		redirect("/blog");
	}
	@Clear
	public void login() {
		String username = getPara("username");
		String pwd = getPara("pwd");
		System.out.println("---"+username);
		System.out.println("---"+pwd);
		WeisiUser weisiUser = getUserInfo(username, pwd);
		if(weisiUser!=null){
			if(null == weisiUser.getSts() || "0".equals(weisiUser.getSts())){
				setAttr("flag","nosts");
				
				redirect("/");
			}else {
				HttpSession session = getSession(); 
				session.setAttribute("username", username);
				redirect("/blog");
			}
		}else {
			setAttr("flag","no");
			System.err.println("-------");
			redirect("/blog/noLogin");
		}
	}
	@Clear
	public void noLogin() {
		System.err.println("1232222222222222");
		setAttr("flag","no");
		render("/index/index.html");
	}
	public void tuichu() {
		HttpSession session = getSession(); 
		session.removeAttribute("username");
		redirect("/");
	}
	public void xiugai() {
		HttpSession session = getSession(); 
		session.removeAttribute("username");
		redirect("/");
	}
	
	public void reg() {
		String username = getPara("username");
		String pwd = getPara("pwd");
		String nc = getPara("nc");
		WeisiUser user = null;
		List<WeisiUser> list = null;
		String sql = "select * from weisiUser where username = ? ";
		list = WeisiUser.dao.find(sql,username);
		if(null != list && list.size()>0){
			setAttr("flag", 1000);//用户名重复
			redirect("/index/reg.html");
		}
		sql = "select * from weisiUser where nc = ? ";
		list = WeisiUser.dao.find(sql,nc);
		if(null != list && list.size()>0){
			setAttr("flag", 2000);//昵称重复
			redirect("/index/reg.html");
		}
		user = new WeisiUser();
		user.setName(username);
		user.setPwd(pwd);
		user.setName(nc);
		user.setSts("0");
		user.save();
		setAttr("flag", 1);//昵称重复
		redirect("/index/index.html");
	}
	
	public WeisiUser getUserInfo(String username,String pwd) {
		String sql = "select * from weisiUser where username = ? and pwd = ? ";
		return WeisiUser.dao.findFirst(sql,username,pwd);
	}
	
}


